import java.io.IOException;
import javax.servlet.*;

/**
 * Фильтр кодировки текстов post-запросов
 * @author Konstantin Chumachenko <kostyachum@gmail.com>
 */
public class CharsetFilter implements Filter {
    // кодировка
    private String requestEncoding;
    private String responseEncoding;

    public void init(FilterConfig config) throws ServletException {

        requestEncoding = config.getInitParameter("requestEncoding");
        if (requestEncoding == null) {
            requestEncoding = "UTF-8";
        }

        responseEncoding = config.getInitParameter("responseEncoding");
        if (responseEncoding == null) {
            responseEncoding = "UTF-8";
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
            throws IOException, ServletException {

        response.setContentType("text/html;charset=" + responseEncoding);
        request.setCharacterEncoding(requestEncoding);

        next.doFilter(request, response);
    }

    public void destroy() {
    }
}